package com.ywl.study.seata.util;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ConcurrentHashMap;

public class DataSourceUtil {

    public static final String JDBC_PRO_PATH="jdbc.properties";

    public static final ConcurrentHashMap<String, DataSource> DATA_SOURCE_MAP=new ConcurrentHashMap<String, DataSource>();

    public static DataSource getDataSource(String name){
        String driverKey = "jdbc." + name + ".driver";
        String urlKey = "jdbc." + name + ".url";
        String userNameKey = "jdbc." + name + ".username";
        String pwdKey = "jdbc." + name + ".password";
        DataSource dataSource=new DruidDataSource();
        ((DruidDataSource) dataSource).setDriverClassName(PropertiesUtil.getPropertieValue(JDBC_PRO_PATH,driverKey));
        ((DruidDataSource) dataSource).setUrl(PropertiesUtil.getPropertieValue(JDBC_PRO_PATH,urlKey));
        ((DruidDataSource) dataSource).setUsername(PropertiesUtil.getPropertieValue(JDBC_PRO_PATH,userNameKey));
        ((DruidDataSource) dataSource).setPassword(PropertiesUtil.getPropertieValue(JDBC_PRO_PATH,pwdKey));
        return new DataSourceProxy(dataSource);
    }


    public static Connection getConnection(String name) throws SQLException {
        DATA_SOURCE_MAP.putIfAbsent(name,getDataSource(name));
        return DATA_SOURCE_MAP.get(name).getConnection();
    }

    public static int executeUpdate(String name,String sql) throws SQLException {
        Connection connection=null;
        Statement statement=null;
        int result=0;
        try {
            connection=getConnection(name);
            statement=connection.createStatement();
            result=statement.executeUpdate(sql);

        }catch (SQLException exx){
            throw exx;
        }finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException exx) {
            }
        }
        return result;
    }



    public static String getSingleResult(String name,String sql) throws SQLException {
        Connection connection=null;
        Statement statement=null;
        String result=null;
        try{
            connection=getConnection(name);
            statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery(sql);
            resultSet.next();
            result= resultSet.getString(1);
        }catch (SQLException e){
            throw e;
        }finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException exx) {
            }
        }
        return result;
    }
}
